<template>
  <div class="create-order">
    <Header title="生成订单" />
    <van-contact-card type="edit" :tel="tel" :name="name" @click="onEdit" />
    <div class="content">
      <div v-for="(item, index) in store.state.orderList" :key="index">
        <van-card :num="item.num" :price="item.price" :title="item.title" :thumb="item.pic" />
      </div>
    </div>
    <div class="pay-wrap">
      <div class="price">
        <span>商品金额</span>
        <span>￥{{ totalPrice }}</span>
      </div>
      <van-button type="primary" class="pay-button" block color="#ffc400" @click="handleCreateOrder">生成订单</van-button>
    </div>
  </div>
</template>

<script>
import Header from "@/components/header.vue";
import { onMounted, reactive, toRefs } from "vue";
import { useStore } from "vuex";
import { showDialog } from 'vant';
import { useRoute, useRouter } from "vue-router";
export default {
  components: {
    Header
  },
  setup() {
    const data = reactive({
      tel: '1527991546',
      name: 'Cyan',
      totalPrice: 0
    })
    const store = useStore()
    const onEdit = () => { 
      router.push('/address')
    }
    const router = useRouter()
    const route = useRoute()
    const initPrice = () => {
      let price = 0
      if (store.state.orderList.length) {
        // 遍历item数据
        store.state.orderList.forEach((item) => {
          price += item.num * item.price
        })
      }
      data.totalPrice = price
    }
    // 初始化用户的地址
    const initUser = () => {
      const userAddress = store.state.userAddress
      if (Array.isArray(userAddress)) {
        userAddress.forEach((item) => {
          if (item.isDefault) {
            data.name = item.name
            data.tel = item.tel
          }
        })
      }
    }
    // 生成订单方法
    const handleCreateOrder = () => {
      showDialog({
        title: '提示',
        message: '生成订单成功！',
      }).then(() => {
        // 要和购物车联系起来
        // 结算多少购物车减少多少
        let newList = store.state.cartList.filter((item) => {
          return route.query.list.includes(item.id + "")

        })
        store.commit('delete', newList)
        store.commit('orderListEd')
        
        router.push('/order')
      });
    }
    onMounted(() => {
      initPrice()
      initUser()
    })
    return {
      ...toRefs(data),
      onEdit,
      store,
      handleCreateOrder
    }
  }
}
</script>

<style lang="less" scoped>
.create-order {
  display: flex;
  flex-flow: column;
  height: 100%;

  .content {
    flex: 1;
    overflow-y: auto;

  }

  .pay-wrap {
    padding: 10px 0;
    width: 100%;
    background: #fff;
    border-top: 1px solid #e9e9e9;

    >div {
      display: flex;
      justify-content: space-between;
      padding: 0 5%;
      margin: 10px 0;
      font-size: 14px;

      span:nth-child(2) {
        color: red;
        font-size: 18px;

      }
    }

    .pay-button {
      width: 90%;
      margin: 0 auto;
    }
  }
}
</style>